﻿//-----------------------------------------------------------------------
// <copyright file="Rotation.cs" company="FCIS">
//     Company copyright tag.
// </copyright>
//-----------------------------------------------------------------------

namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;

    /// <summary>
    /// rotation operation in image
    /// </summary>
    public class Rotation
    {
        /// <summary>
        /// static object of this class to use this without create object
        /// </summary>
        private static Rotation rotation = new Rotation();

        /// <summary>
        /// buffer of input image
        /// </summary>
        private Color[,] bufferOfImage;

        /// <summary>
        /// buffer after operation
        /// </summary>
        private Color[,] newBufferOfImage;

        /// <summary>
        /// Gets or sets the rotation1.
        /// </summary>
        /// <value>
        /// The rotation1.
        /// </value>
        public static Rotation Rotation1
        {
            get { return Rotation.rotation; }
            set { Rotation.rotation = value; }
        }

        /// <summary>
        /// Makes the rotate of image.
        /// </summary>
        /// <param name="image">The image need mmake rotate in it.</param>
        /// <param name="seta">The angle of rotate.</param>
        /// <returns>
        /// the new image
        /// </returns>
        public Bitmap MakeRotate(Bitmap image, double seta)
        {
            Bitmap new_image = new Bitmap(image.Width, image.Height);
            try
            {
                this.bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
                this.newBufferOfImage = new Color[this.bufferOfImage.GetLength(0), this.bufferOfImage.GetLength(1)];
                double x;
                double y;
                for (int i = 0; i < this.bufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < this.bufferOfImage.GetLength(0); j++)
                    {
                        x = (j * Math.Cos(seta)) + (i * Math.Sin(seta));
                        y = (j * -1 * Math.Sin(seta)) + (i * Math.Cos(seta));
                        int new_x = (int)x;
                        int new_y = (int)y;
                        if (new_x < this.bufferOfImage.GetLength(0) && new_y < this.bufferOfImage.GetLength(1) && new_x >= 0 && new_y >= 0)
                        {
                            this.newBufferOfImage[new_x, new_y] = this.bufferOfImage[j, i];
                            new_image.SetPixel(new_x, new_y, this.newBufferOfImage[new_x, new_y]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }

            return new_image;
        }
    }
}
